
package com.netease.xone.fragment;

import preferMgr.XonePrefHelper;
import protocol.XoneCallBack;
import protocol.XoneService;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.netease.xone.R;

public class FragSetReceiveMsg extends FragmentBase {

    private RadioGroup mRadioGroupMsg, mRadioGroupComment;
    private long mOriTime;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        getActivity().setTitle(R.string.setting_item_msg);
        View view = inflater.inflate(R.layout.fragment_msg_set, container, false);
        findViews(view);
        return view;
    }

    private void findViews(View view) {
        TextView tv = (TextView)view.findViewById(R.id.frag_setmsg_category_msg);
        tv.setText(R.string.receive_msg_tip);
        tv = (TextView)view.findViewById(R.id.frag_setmsg_category_comment);
        tv.setText(R.string.receive_comment_tip);

        mRadioGroupMsg = (RadioGroup)view.findViewById(R.id.receive_msg_group);
        mRadioGroupComment = (RadioGroup)view.findViewById(R.id.receive_comment_group);
        mOriTime = XonePrefHelper.getSettingUpdateTime(getActivity());
        
        int id = R.id.receive_msg_all;
        boolean isAll = XonePrefHelper.getReceiveAllMessage(getActivity());
        if (!isAll) {
            id = R.id.receive_msg_followed;
        }
        mRadioGroupMsg.check(id);
        mRadioGroupMsg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.receive_msg_all:
                        XonePrefHelper.putReceiveAllMessage(getActivity(), true);
                        break;
                    case R.id.receive_msg_followed:
                        XonePrefHelper.putReceiveAllMessage(getActivity(), false);
                        break;

                }
                XonePrefHelper.putSettingUpdateTime(getActivity(), System.currentTimeMillis());
            }
        });
        id = R.id.receive_comment_all;
        isAll = XonePrefHelper.getReceiveAllComment(getActivity());
        if (!isAll) {
            id = R.id.receive_comment_followed;
        }
        mRadioGroupComment.check(id);
        mRadioGroupComment.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.receive_comment_all:
                        XonePrefHelper.putReceiveAllComment(getActivity(), true);
                        break;
                    case R.id.receive_comment_followed:
                        XonePrefHelper.putReceiveAllComment(getActivity(), false);
                        break;

                }
                XonePrefHelper.putSettingUpdateTime(getActivity(), System.currentTimeMillis());
            }
        });
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        XoneService.getInstance().addListener(mXoneCallBack);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        XoneService.getInstance().removeListener(mXoneCallBack);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mOriTime != XonePrefHelper.getSettingUpdateTime(getActivity())) {
            XoneService.getInstance().doSetAppSetting();
        }
    }

    private void refreshViewState() {
        if (mRadioGroupMsg == null || mRadioGroupComment == null)
            return;
        mOriTime = XonePrefHelper.getSettingUpdateTime(getActivity());
        int id = R.id.receive_msg_all;
        boolean isAll = XonePrefHelper.getReceiveAllMessage(getActivity());
        if (!isAll) {
            id = R.id.receive_msg_followed;
        }
        mRadioGroupMsg.check(id);

        id = R.id.receive_comment_all;
        isAll = XonePrefHelper.getReceiveAllComment(getActivity());
        if (!isAll) {
            id = R.id.receive_comment_followed;
        }
        mRadioGroupComment.check(id);

    }

    private XoneCallBack mXoneCallBack = new XoneCallBack() {

        @Override
        public void onGetAppSetting(int transactionId) {
            refreshViewState();
        };

    };
}
